package org.jeecg.modules.pdd.util;

import cn.hutool.http.HttpUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

public class DingDanXiaUtil {

    public static String dingdanxia_appkey = "NclO6PgM7dUjHmch6g6XXZORWAy8uRzs";

    public static String getDetail(String goodsId) {
        String url = "http://api.tbk.dingdanxia.com/pdd/goods_detail?apikey=" + dingdanxia_appkey + "&goods_id_list=" + goodsId;

        String resultStr = "";
        try {
            resultStr = HttpUtil.get(url);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultStr;
    }


    public static String getShateUrl(String p_id, Long goods_id_list) {

        String url = "http://api.tbk.dingdanxia.com/pdd/convert";
        Map<String, Object> map = new HashMap<>();
        map.put("apikey", DingDanXiaUtil.dingdanxia_appkey);
        map.put("p_id", p_id);
        map.put("goods_id_list", goods_id_list);
        map.put("generate_short_url", true);
        map.put("generate_mall_collect_coupon", true);


        String resultStr = "";
        try {
            resultStr = HttpUtil.post(url, map);
            //  resultStr = HttpUtil.get(url);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultStr;
    }


    /**
     * 默认值 ： 0 offset是limit的整数倍。 比如 10，20，30，40
     *
     * @param p_id
     * @param list_id
     * @param limit
     * @param sort_type
     * @return
     */
    public static String getPddGoodsData(String p_id, String list_id, Integer limit, Integer sort_type, Integer offset) {

        String url = "http://api.tbk.dingdanxia.com/pdd/goodslist";
        Map<String, Object> map = new HashMap<>();
        map.put("apikey", DingDanXiaUtil.dingdanxia_appkey);
        map.put("p_id", p_id);
        if (!StringUtils.isEmpty(list_id)) {
            map.put("list_id", list_id);
        }

        if (null != limit) {
            map.put("limit", limit);
        }
        if (null != sort_type) {
            //1-实时热销榜；2-实时收益榜
            map.put("sort_type", sort_type);
        }
        if (null != offset) {
            map.put("offset", offset);
        }
        String resultStr = "";
        try {
            resultStr = HttpUtil.post(url, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultStr;

    }

    /**
     * 通过目录检索商品信息
     *
     * @param cat_id
     * @param page
     * @return
     */
    public static String getPddGoodsByCatId(Long cat_id, Integer page,Integer page_size) {

        String url = "http://api.tbk.dingdanxia.com/pdd/goods_search";
        Map<String, Object> map = new HashMap<>();
        map.put("apikey", DingDanXiaUtil.dingdanxia_appkey);
        map.put("cat_id", cat_id);
        if (null != page) {
            map.put("page", page);
        }
        if (null != page_size) {
            map.put("page_size", page_size);
        }


        String resultStr = "";
        try {
            resultStr = HttpUtil.post(url, map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultStr;

    }


    public static String getOrders(Long start_update_time, Long end_update_time) {

        String url = "http://api.tbk.dingdanxia.com/pdd/orderlist";
        Map<String, Object> map = new HashMap<>();
        map.put("apikey", DingDanXiaUtil.dingdanxia_appkey);
        map.put("start_update_time", start_update_time);
        map.put("end_update_time", end_update_time);
        map.put("page_size", 100);
        map.put("page", 1);


        String resultStr = "";
        try {
            resultStr = HttpUtil.post(url, map);
            //  resultStr = HttpUtil.get(url);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return resultStr;
    }


    public static void main(String[] args) {

        //   System.out.println(getDetail("72838410785"));

        System.out.println(getPddGoodsData("10469205_143269153", null, null, null, null));
    }

}
